package com.mingserve.raw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.mingserve.common.core.utils.SpringUtils;
import com.mingserve.common.core.utils.StringUtils;
import com.mingserve.raw.domain.*;
import com.mingserve.raw.event.RefreshEvent;
import com.mingserve.raw.mapper.*;
import com.mingserve.raw.service.IPointService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 测点管理Service接口实现
 *
 * @author lvwshuai
 * @date 2024-02-02
 */
@RequiredArgsConstructor
@Service
public class PointServiceImpl implements IPointService
{
    private final PointMapper pointMapper;
    private final PointConfigMapper pointConfigMapper;
    private final PointFaultMapper pointFaultMapper;
    private final PointBearingMapper pointBearingMapper;
    private final PointDeviceMapper pointDeviceMapper;

    /**
     * 批量新增或更新测点
     *
     * @param list 测点列表
     * @return 结果
     */
    @Override
    public Boolean batchPoint(List<Point> list) {
        pointMapper.delete(new QueryWrapper<>());
        return pointMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询测点列表
     *
     * @return 结果
     */
    @Override
    public List<Point> queryList() {
        return pointMapper.selectList();
    }

    /**
     * 查询设备下测点列表
     *
     * @return 结果
     */
    @Override
    public List<Point> queryByEquipment(Equipment equipment) {
        LambdaQueryWrapper<Point> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(equipment.getEquipmentCode()))
            lqw.eq(Point::getEquipmentCode, equipment.getEquipmentCode());
        return pointMapper.selectList(lqw);
    }

    /**
     * 按编码查测点详情
     *
     * @param pointCode
     * @return
     */
    @Override
    public Point getPointByCode(String pointCode) {
        return pointMapper.selectById(pointCode);
    }

    /**
     * 修改测点信息
     *
     * @param point
     * @return
     */
    @Transactional
    @Override
    public int updatePoint(Point point) {
        Point find = pointMapper.selectById(point.getPointCode());
        if (find == null) return 0;
        LambdaUpdateWrapper<Point> luw = new LambdaUpdateWrapper<>();
        luw.eq(Point::getPointCode, point.getPointCode());
        luw.set(Point::getAxial, point.getAxial());
        RefreshEvent refreshEvent = new RefreshEvent();
        refreshEvent.setRefreshPoint(false);
        // IoT编码不为空且发生变化
        if (StringUtils.isNotEmpty(point.getDeviceCode()) && !point.getDeviceCode().equals(find.getDeviceCode())) {
            luw.set(Point::getDeviceCode, point.getDeviceCode());
            refreshEvent.setRefreshPoint(true);
            // 解除别的测点绑定
            LambdaUpdateWrapper<Point> luw2 = new LambdaUpdateWrapper<>();
            luw2.eq(Point::getDeviceCode, point.getDeviceCode());
            List<Point> pointList = pointMapper.selectList(luw2);
            for (Point item : pointList) {
                item.setDeviceCode("");
                pointMapper.updateById(item);
                // 记录别的测点解绑记录
                insertPointDevice(item);
            }
        } else {
            // 之前已设置，进行解绑操作
            if (StringUtils.isNotEmpty(find.getDeviceCode())) {
                luw.set(Point::getDeviceCode, point.getDeviceCode());
                refreshEvent.setRefreshPoint(true);
            }
        }
        int rows = pointMapper.update(luw);
        if (refreshEvent.isRefreshPoint()) {
            // 记录变更记录
            insertPointDevice(point);
        }
        // 推送测点列表缓存刷新
        SpringUtils.publishEvent(refreshEvent);
        return rows;
    }

    /**
     * 批量新增或更新测点配置
     *
     * @param list 测点配置
     * @return 结果
     */
    @Override
    public Boolean batchPointConfig(List<PointConfig> list) {
        pointConfigMapper.delete(new QueryWrapper<>());
        return pointConfigMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询测点配置
     *
     * @return 结果
     */
    @Override
    public List<PointConfig> queryConfigList() {
        return pointConfigMapper.selectList();
    }

    /**
     * 批量新增或更新测点诊断参数
     *
     * @param list 测点配置
     * @return 结果
     */
    @Override
    public Boolean batchPointFault(List<PointFault> list) {
        pointFaultMapper.delete(new QueryWrapper<>());
        return pointFaultMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询测点配置
     *
     * @return 结果
     */
    @Override
    public List<PointFault> queryFaultList() {
        return pointFaultMapper.selectList();
    }

    /**
     * 批量新增或更新测点轴承信息
     *
     * @param list 测点配置
     * @return 结果
     */
    @Override
    public Boolean batchPointBearing(List<PointBearing> list) {
        pointBearingMapper.delete(new QueryWrapper<>());
        return pointBearingMapper.insertOrUpdateBatch(list);
    }

    /**
     * 查询测点轴承信息
     *
     * @return 结果
     */
    @Override
    public List<PointBearing> queryBearingList() {
        return pointBearingMapper.selectList();
    }

    /**
     * 写入测点绑定IoT
     *
     * @param point
     * @return
     */
    @Override
    public int insertPointDevice(Point point) {
        PointDevice pointDevice = new PointDevice();
        pointDevice.setPointCode(point.getPointCode());
        pointDevice.setDeviceCode(point.getDeviceCode());
        return pointDeviceMapper.insert(pointDevice);
    }

    /**
     * 查询测点故障配置
     *
     * @param pointCode
     * @return
     */
    @Override
    public PointFault getPointFaultByCode(String pointCode) {
        return pointFaultMapper.selectById(pointCode);
    }
}
